package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.domain.ModelBean;
import com.bezdelnick.forum.domain.Question;
import com.bezdelnick.forum.domain.Subtype;
import com.bezdelnick.forum.domain.Type;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.LinkWithText;
import com.bezdelnick.forum.web.component.QuestionsListView;
import com.bezdelnick.forum.web.model.NavigationTreeModel;
import com.bezdelnick.forum.web.page.template.UserMainMenuAndLeftMenuTemplate;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 13:58
 * To change this template use File | Settings | File Templates.
 */
public class IndexPage extends UserMainMenuAndLeftMenuTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexPage.class);

    private static final String TREE_ROOT_NAME = "Types answers";

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    public List<Question> questions;

    private BaseTree navigationTree;
    private QuestionsListView questionsListView;

    public IndexPage() {

        questions = forumService.getQuestionDao().loadQuestions();

        // add menu component
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new ModelBean(TREE_ROOT_NAME));
        NavigationTreeModel navigationTreeModel = new NavigationTreeModel(rootNode);

        navigationTree = new LinkTree("menu", navigationTreeModel) {
            @Override
            protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
                super.onNodeLinkClicked(node, tree, target);

                TreeNode _node = (TreeNode) node;

                if (!_node.isLeaf()) {
                    if (!_node.toString().equals(TREE_ROOT_NAME)) {
                        Type type = forumService.getTypeDao().getTypeByName(_node.toString());
                        if (type != null) {
                            questions = forumService.getQuestionDao().loadQuestionsByTypeId(type.getId());
                        }
                    } else {
                        questions = forumService.getQuestionDao().loadQuestions();
                    }

                    tree.updateTree(target);
                } else {
                    Subtype subtype = forumService.getSubtypeDao().getSubtypeByName(_node.toString());
                    if (subtype != null) {
                        questions = forumService.getQuestionDao().loadQuestionsBySubtypeId(subtype.getId());
                    }
                }
            }
        };

        add(navigationTree);
        navigationTree.getTreeState().expandAll();

        // add body component

        add(new LinkWithText("sort_1", "#", "Sort 1"));
        add(new LinkWithText("sort_2", "#", "Sort 2"));
        add(new LinkWithText("sort_3", "#", "Sort 3"));

        questionsListView = new QuestionsListView("list_questions", new PropertyModel(this, "questions"), ForumService.MODE.USER);
        add(questionsListView);
    }
}
